package com.pearadmin.modules.sys.controller.work;

import com.github.pagehelper.PageInfo;
import com.pearadmin.common.constant.ControllerConstant;
import com.pearadmin.common.tools.SecurityUtil;
import com.pearadmin.common.tools.SequenceUtil;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.modules.sys.domain.SysUser;
import com.pearadmin.modules.sys.domain.WorkCar;
import com.pearadmin.modules.sys.service.IWorkCarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;

/**
 * 车队工作记录Controller
 *
 * @author Mr.Ling
 * @date 2021-11-26
 */
@RestController
@Api(tags = {"车队工作记录"})
@RequestMapping(ControllerConstant.API_WORK_PREFIX + "car")
public class WorkCarController extends BaseController {
    private String prefix = "work/car" ;

    @Autowired
    private IWorkCarService workCarService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/work/car/main','work:car:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询车队工作记录列表
     */
    @ResponseBody
    @ApiOperation("车队记录列表")
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/work/car/data','work:car:data')")
    public ResultTable list(@ModelAttribute WorkCar workCar, PageDomain pageDomain) {
        PageInfo<WorkCar> pageInfo = workCarService.selectWorkCarPage(workCar, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增车队工作记录
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/work/car/add','work:car:add')")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存车队工作记录
     */
    @ResponseBody
    @ApiOperation("车队记录保存")
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/work/car/add','work:car:add')")
    public Result save(@RequestBody WorkCar workCar) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        workCar.setCreateTime(LocalDateTime.now());
        workCar.setCreateBy(sysUser.getUserId());
        workCar.setCreateName(sysUser.getRealName());
        workCar.setId(SequenceUtil.makeStringId());
        return decide(workCarService.insertWorkCar(workCar));
    }

    /**
     * 修改车队工作记录
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/work/car/edit','work:car:edit')")
    public ModelAndView edit(String id, ModelMap mmap) {
        WorkCar workCar = workCarService.selectWorkCarById(id);
        workCar.setUpdateTime(LocalDateTime.now());
        mmap.put("workCar", workCar);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存车队工作记录
     */
    @ResponseBody
    @PutMapping("/update")
    @ApiOperation("车队记录修改")
    @PreAuthorize("hasPermission('/work/car/edit','work:car:edit')")
    public Result update(@RequestBody WorkCar workCar) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        workCar.setUpdateTime(LocalDateTime.now());
        workCar.setUpdateBy(sysUser.getUserId());
        workCar.setUpdateName(sysUser.getUsername());
        return decide(workCarService.updateWorkCar(workCar));
    }

    /**
     * 删除车队工作记录
     */
    @ResponseBody
    @ApiOperation("批量刪除")
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/work/car/remove','work:car:remove')")
    public Result batchRemove(String ids) {
        return decide(workCarService.deleteWorkCarByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @ApiOperation("根据车队记录id 删除")
    @PreAuthorize("hasPermission('/work/car/remove','work:car:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(workCarService.deleteWorkCarById(id));
    }
}
